package com.lib.base.utils.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;

import com.lib.base.R;
import com.lib.base.utils.ImageUtil;

public class LoadingDialog extends Dialog {

    /**
     * LoadDialog
     */
    private static LoadingDialog loadDialog;

    /**
     * 点击系统返回按钮是否消失
     */
    private boolean isCancel;

    /**
     * the LoadDialog constructor
     *
     * @param ctx        Context
     * @param isTouchOut boolean : true-->返回不消失，false-->按返回dismiss
     */
    public LoadingDialog(Context ctx, boolean isTouchOut, boolean isCancel) {
        super(ctx, R.style.LoadingDialog);
//        this.getContext().setTheme(android.R.style.Theme_InputMethod);
        setContentView(R.layout.dialog_loading);
        ImageView imgProgress = findViewById(R.id.imgProgress);
        ImageUtil.Companion.getInstance().loadImageRes(ctx,imgProgress,R.mipmap.loading);

        /* 点击返回按钮是否dismiss */
        this.isCancel = isCancel;
        /* 点击外部是否dismiss */
        setCanceledOnTouchOutside(isTouchOut);

        Window window = getWindow();
        WindowManager.LayoutParams attributesParams = window.getAttributes();
        attributesParams.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        attributesParams.dimAmount = 0.5f;//背景透明度
//        attributesParams.dimAmount = 0f;//背景透明度
        window.setAttributes(attributesParams);
        window.setBackgroundDrawableResource(android.R.color.transparent);// 一句话搞定

        window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (!this.isCancel) {
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * show the mDialogTextView
     *
     * @param context
     */
    public static void show(Context context) {
        showDialog(context, true, false);
    }

    /**
     * show the mDialogTextView
     *
     * @param context  Context
     * @param isCancel boolean
     */
    public static void show(Context context, boolean isTouchOut, boolean isCancel) {
        showDialog(context, isTouchOut, isCancel);
    }

    /**
     * show the mDialogTextView
     *
     * @param context    Context
     * @param isTouchOut boolean, true is can't dimiss，false is can dimiss
     */
    private static void showDialog(Context context, boolean isTouchOut, boolean isCancel) {
        if (context instanceof Activity) {
            if (((Activity) context).isFinishing()) {
                return;
            }
        }
        if (loadDialog != null && loadDialog.isShowing()) {
            return;
        }
        loadDialog = new LoadingDialog(context, isTouchOut, isCancel);
        loadDialog.show();
    }

    /**
     * dismiss the mDialogTextView
     */
    public static void dismiss(Context context) {
        try {
            if (context instanceof Activity) {
                if (((Activity) context).isFinishing()) {
                    loadDialog = null;
                    return;
                }
            }

            if (loadDialog != null && loadDialog.isShowing()) {
                Context loadContext = loadDialog.getContext();
                if (loadContext != null && loadContext instanceof Activity) {
                    if (((Activity) loadContext).isFinishing()) {
                        loadDialog = null;
                        return;
                    }
                }
                loadDialog.dismiss();
                loadDialog = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            loadDialog = null;
        }
    }
}
